//
//  AppDelegate.m
//  NVAReader
//
//  Created by DuyLK on 6/4/14.
//  Copyright (c) 2014 com.nva.nvareader. All rights reserved.
//

#import "AppDelegate.h"
#include "common.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSString *filename;
	queue = dispatch_queue_create("com.nva.nvareader", NULL);
    ctx = fz_new_context(NULL, NULL, 1024<<20);
	fz_register_document_handlers(ctx);
}

-(void) applicationWillBecomeActive:(NSNotification *)notification{
    
}


-(void) applicationDidBecomeActive:(NSNotification *)notification{
    
}


-(IBAction)click:(id)sender
{
    [self openDocument:@"Cocoa and Objective-C Cookbook.pdf"];
}


- (void) openDocument: (NSString*)nsfilename
{
	NSString *nspath = @"/Users/duylk/Desktop/Cocoa and Objective-C Cookbook.pdf";
	_filePath = malloc(strlen([nspath UTF8String])+1);
	if (_filePath == NULL) {
		//showAlert(@"Out of memory in openDocument", nsfilename);
		return;
	}
    
	strcpy(_filePath, [nspath UTF8String]);
    
	dispatch_sync(queue, ^{});
    
	printf("open document '%s'\n", _filePath);
    
	_filename = [nsfilename retain];
	[doc release];
	doc = [[DocRef alloc] initWithFilename:_filePath];
	if (!doc) {
		//showAlert(@"Cannot open document", nsfilename);
		return;
	}
    
	if (fz_needs_password(doc->doc))
		[self askForPassword: @"'%@' needs a password:"];
	else
		[self onPasswordOkay];
}


- (void) askForPassword: (NSString*)prompt
{
    
}


- (void) onPasswordOkay
{
    
}

- (void) onPasswordCancel
{
    
}

- (void) reload
{
    
}

-(void) dealloc{
    dispatch_release(queue);
    [super dealloc];
}

@end
